﻿/*
 * Created by SharpDevelop.
 * User: Dhanesh
 * Date: 13-10-2008
 * Time: 01:04:51
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.Xml.Xsl;
using System.Diagnostics;

namespace ChallanSystem
{
	/// <summary>
	/// Description of Orders.
	/// </summary>
	public partial class Orders : Form
	{
		XmlDocument xdoc = null;
		int orderID = 0;
		Customer cust = null;
		Transporter trans = null;
		Order_Products ordprod = null;
		DataSet ds = null;
		
		DataSet dsCust = null;
		DataSet dsTrans = null;
		
		DataTable dt = null;
		
		public Orders()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void OrdersLoad(object sender, EventArgs e)
		{
			cust = new Customer();
			dsCust = cust.View();
			if(dsCust.Tables[0].Rows.Count > 0)
			{
				foreach (DataRow dr in dsCust.Tables[0].Rows)
				{
					cboCustomers.Items.Add(dr["Customer_Name"].ToString());
				}
			}
			
			trans = new Transporter();
			dsTrans = trans.View();
			if(dsTrans.Tables[0].Rows.Count > 0)
			{
				foreach (DataRow dr in dsTrans.Tables[0].Rows)
				{
					cboTransporters.Items.Add(dr["Transporter_Name"].ToString());
				}
			}
			
			ds = new DataSet();
			dt = new DataTable("Order_Products");
			dt.Columns.Add("Product_ID");
			dt.Columns.Add("Product_Name");
			dt.Columns.Add("Quantity");
			dt.Columns.Add("Price");
			dt.Columns.Add("Total");
			ds.Tables.Add(dt);
			
		}
		
		void BtnAddProductClick(object sender, EventArgs e)
		{
			ordprod = new Order_Products();
			//ordprod.ParentForm = this;
			if(cboCustomers.SelectedIndex < 0)
			{
				MessageBox.Show("Please Select A Customer");
			}
			else
			{
				ordprod.Customer_Type = Convert.ToInt32(dsCust.Tables[0].DefaultView[cboCustomers.SelectedIndex].Row["Cust_Typ_Lkp_ID"].ToString());
				DialogResult = ordprod.ShowDialog();
			//Array a = ordprod.formArray;
			if(DialogResult == DialogResult.OK)
			{
				//	MessageBox.Show("length a :- ", Convert.ToString(a.Length));
				//int i = formArray.Length;
//				string temp = string.Empty;
//				foreach (object a in ordprod.formArray)
//				{
//					temp += a.ToString()+"\n";
//				}
				ds.Tables[0].Rows.Add(ordprod.formArray);
				datagridProducts.SetDataBinding(ds,"Order_Products");
				
//				try
//				{
//					
//					XmlDocument xmlDoc = new XmlDocument();
//					xmlDoc.CreateElement("Customer");
///					XmlNode xnode = xmlDoc.DocumentElement.FirstChild;
//					xnode.InnerText = "Dhanesh";
//					xmlDoc.CreateElement("Transporter");
//					xmlDoc.InnerText = "Kenil";
//					xmlDoc.Load(Save("c:\\test.xml");
//					XslCompiledTransform xslt = new XslCompiledTransform();
//					xslt.Load("c:\\catalog.xsl");
//					xslt.Transform("c:\\catalog.xml", "c:\\result.html");
//				}
//				catch(Exception e1)
//				{
//					MessageBox.Show(e1.ToString());
//				}
//				ds.Tables["Order_Products"].WriteXml("c:\\bill.xml");
//				MessageBox.Show(temp);
			}
			
			}
//			datagridProducts.DataSource = formArray;
			//datagridProducts.DataBindings();
		}
		
		public static Array formArray ;
		public  Array formDetails
		{
			get { return formArray; }
			set
			{
				//MessageBox.Show("In Setter ");
				formArray = value;
				int i = formArray.Length;
				//MessageBox.Show(Convert.ToString(i));
			}
		}

		
		void BtnSaveClick(object sender, EventArgs e)
		{
			if(validate())
			{
				if(isNumericCheck())
				{
					try
					{
						//dsCust.Tables[0].Rows[cboCustomers.SelectedIndex];
						//MessageBox.Show(cboCustomers.SelectedIndex.ToString());
						int custID = Int32.Parse(dsCust.Tables[0].Rows[cboCustomers.SelectedIndex].ItemArray[0].ToString());
						int transID = Int32.Parse(dsTrans.Tables[0].Rows[cboTransporters.SelectedIndex].ItemArray[0].ToString());
						int LRNO = Convert.ToInt32(txtLrno.Text);
						
						Order or = new Order();
						or.CustomerID = custID;
						or.TransporterID = transID;
						or.LRNo = LRNO;
						or.Products = ds;
						orderID = or.Add();
						
						string xmlstr = string.Empty;
						xmlstr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
						xmlstr += "<?xml-stylesheet type=\"text/xsl\" href=\""+Application.StartupPath+"\\style\\OrderForm.xsl\"?>";
						xmlstr += "<order>\n";
						xmlstr += "<id>" + orderID + "</id>\n";
						xmlstr += "<date>" + DateTime.Today + "</date>\n";
						xmlstr += "<customer>\n";
						xmlstr += "<name>" + dsCust.Tables[0].Rows[cboCustomers.SelectedIndex].ItemArray[1].ToString() + "</name>\n";
						xmlstr += "<address>" + dsCust.Tables[0].Rows[cboCustomers.SelectedIndex].ItemArray[2].ToString() + " " + dsCust.Tables[0].Rows[cboCustomers.SelectedIndex].ItemArray[3].ToString() + "\n" + dsCust.Tables[0].Rows[cboCustomers.SelectedIndex].ItemArray[4].ToString() + " " + dsCust.Tables[0].Rows[cboCustomers.SelectedIndex].ItemArray[5].ToString() + "</address>\n";
						xmlstr += "</customer>\n";
						xmlstr += "<transporter>\n";
						xmlstr += "<name>" + dsTrans.Tables[0].Rows[cboTransporters.SelectedIndex].ItemArray[1].ToString() + "</name>\n";
						xmlstr += "</transporter>\n";
						xmlstr += "<lrno>" + LRNO + "</lrno>\n";
						xmlstr += "<products>\n";
						foreach(DataRow drox in ds.Tables[0].Rows)
						{
							xmlstr += "<product>\n";
							xmlstr += "<quantity>" + drox["Quantity"].ToString() + "</quantity>\n";
							xmlstr += "<name>" + drox["Product_Name"].ToString() + "</name>\n";
							xmlstr += "<rate>" + drox["Price"].ToString() + "</rate>\n";
							xmlstr += "<amount>" + drox["Total"].ToString() + "</amount>\n";
							xmlstr += "</product>\n";
						}
						xmlstr += "</products>\n";
						xmlstr += "</order>";

						xdoc = new XmlDataDocument();
						xdoc.LoadXml(xmlstr);
						xdoc.Save(Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)+ "\\RPOrder_"+orderID+".xml");
						
						if(orderID != 0)
						{
							MessageBox.Show("Order Created with Order Id : "+orderID);
						}
						else
						{
							MessageBox.Show("Sorry... Cannot Process Your Request");
						}
						btnPrint.Visible = true;
					}
					catch(Exception)
					{
						MessageBox.Show("Cannot Save The Order !!!");
					}
				}
				else
					MessageBox.Show("Please Enter Valid Data !!!");
			}
			else
				MessageBox.Show("All Fields Are Mandatory !!!");
		}
		
		
		void BtnPrintClick(object sender, EventArgs e)
		{
			XslCompiledTransform xslt = new XslCompiledTransform();

			xslt.Load(Application.StartupPath+"\\style\\orderform.xsl");
			xslt.Transform(Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)+"\\RPOrder_"+orderID+".xml", "c:\\result.html");
			Process proc = new Process();
			ProcessStartInfo psi = proc.StartInfo;
			psi.FileName = "c:\\result.html";
			proc.Start();
		}
		
		bool validate()
		{
			bool validate = true;
			
			if(txtLrno.Text == "" || cboCustomers.SelectedIndex < 0 || cboTransporters.SelectedIndex <0 || ds.Tables[0].Rows.Count == 0 )
				validate = false;
			else
				validate = true;
			
			return validate;
		}
		bool isNumericCheck()
		{
			bool validate = true;
			
			try
			{
				uint intValue = Convert.ToUInt32(txtLrno.Text);
			}
			catch(Exception)
			{
				validate = false;
			}
			return validate;
		}
		
		
		void Button1Click(object sender, EventArgs e)
		{
			this.Close();
			ds.Dispose();
			dsCust.Dispose();
			dsTrans.Dispose();
			this.DialogResult = DialogResult.Cancel; 
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			Form1 viewOrder = new Form1();
			//MessageBox.Show("Microsoft Warning : Reason Code - 566 \nSorry Unable to Proceed \nNeed a Higher Version of Grid Control", "Microsoft Warning...");
			viewOrder.ShowDialog();
		}
	}

}
